package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_wjjhl.domain.WdclzdgmdBaDetail;
import com.ruoyi.report_wjjhl.domain.vo.WdclzdgmdBaDetailVo;
import com.ruoyi.report_wjjhl.mapper.WdclzdgmdBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdclzdgmdbgBsInfoBo;
import com.ruoyi.report_wjjhl.domain.vo.WdclzdgmdbgBsInfoVo;
import com.ruoyi.report_wjjhl.domain.WdclzdgmdbgBsInfo;
import com.ruoyi.report_wjjhl.mapper.WdclzdgmdbgBsInfoMapper;
import com.ruoyi.report_wjjhl.service.IWdclzdgmdbgBsInfoService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 最大干密度最佳含水率试验检测报告，详细使用对应记录的详细Service业务层处理
 *
 * @author zyb
 * @date 2024-07-31
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class WdclzdgmdbgBsInfoServiceImpl implements IWdclzdgmdbgBsInfoService {

    private final WdclzdgmdbgBsInfoMapper baseMapper;
    private final WdclzdgmdBaDetailMapper detailMapper;

    /**
     * 查询最大干密度最佳含水率试验检测报告，详细使用对应记录的详细
     */
    @Override
    @FIllBasicInfo
    public WdclzdgmdbgBsInfoVo queryById(Long excelId){
        WdclzdgmdbgBsInfoVo wdclzdgmdbgBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<WdclzdgmdBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<WdclzdgmdBaDetail>()
            .eq(WdclzdgmdBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            wdclzdgmdbgBsInfoVo.setBaDetailVos(baDetailVos);
        }
        // 如果已经有值了则不需要计算结论。
        if (StrUtil.isNotEmpty(wdclzdgmdbgBsInfoVo.getJiancjl()) && !"检测结论：".equals(wdclzdgmdbgBsInfoVo.getJiancjl())) {
            return wdclzdgmdbgBsInfoVo;
        }
        return jcjl(wdclzdgmdbgBsInfoVo);
    }

    /**
     * 查询最大干密度最佳含水率试验检测报告，详细使用对应记录的详细列表
     */
    @Override
    public TableDataInfo<WdclzdgmdbgBsInfoVo> queryPageList(WdclzdgmdbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdclzdgmdbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<WdclzdgmdbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询最大干密度最佳含水率试验检测报告，详细使用对应记录的详细列表
     */
    @Override
    public List<WdclzdgmdbgBsInfoVo> queryList(WdclzdgmdbgBsInfoBo bo) {
        LambdaQueryWrapper<WdclzdgmdbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdclzdgmdbgBsInfo> buildQueryWrapper(WdclzdgmdbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdclzdgmdbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, WdclzdgmdbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, WdclzdgmdbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), WdclzdgmdbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), WdclzdgmdbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), WdclzdgmdbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), WdclzdgmdbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), WdclzdgmdbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), WdclzdgmdbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), WdclzdgmdbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), WdclzdgmdbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancrq()), WdclzdgmdbgBsInfo::getJiancrq, bo.getJiancrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), WdclzdgmdbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), WdclzdgmdbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), WdclzdgmdbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), WdclzdgmdbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), WdclzdgmdbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getQuywz()), WdclzdgmdbgBsInfo::getQuywz, bo.getQuywz());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitrwbh()), WdclzdgmdbgBsInfo::getWeitrwbh, bo.getWeitrwbh());
        lqw.eq(StringUtils.isNotBlank(bo.getTuycd()), WdclzdgmdbgBsInfo::getTuycd, bo.getTuycd());
        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), WdclzdgmdbgBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), WdclzdgmdbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), WdclzdgmdbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), WdclzdgmdbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), WdclzdgmdbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), WdclzdgmdbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), WdclzdgmdbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), WdclzdgmdbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), WdclzdgmdbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg1()), WdclzdgmdbgBsInfo::getCailmcgg1, bo.getCailmcgg1());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg2()), WdclzdgmdbgBsInfo::getCailmcgg2, bo.getCailmcgg2());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg3()), WdclzdgmdbgBsInfo::getCailmcgg3, bo.getCailmcgg3());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg4()), WdclzdgmdbgBsInfo::getCailmcgg4, bo.getCailmcgg4());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg5()), WdclzdgmdbgBsInfo::getCailmcgg5, bo.getCailmcgg5());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg6()), WdclzdgmdbgBsInfo::getCailmcgg6, bo.getCailmcgg6());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg7()), WdclzdgmdbgBsInfo::getCailmcgg7, bo.getCailmcgg7());
        lqw.eq(StringUtils.isNotBlank(bo.getChand1()), WdclzdgmdbgBsInfo::getChand1, bo.getChand1());
        lqw.eq(StringUtils.isNotBlank(bo.getChand2()), WdclzdgmdbgBsInfo::getChand2, bo.getChand2());
        lqw.eq(StringUtils.isNotBlank(bo.getChand3()), WdclzdgmdbgBsInfo::getChand3, bo.getChand3());
        lqw.eq(StringUtils.isNotBlank(bo.getChand4()), WdclzdgmdbgBsInfo::getChand4, bo.getChand4());
        lqw.eq(StringUtils.isNotBlank(bo.getChand5()), WdclzdgmdbgBsInfo::getChand5, bo.getChand5());
        lqw.eq(StringUtils.isNotBlank(bo.getChand6()), WdclzdgmdbgBsInfo::getChand6, bo.getChand6());
        lqw.eq(StringUtils.isNotBlank(bo.getChand7()), WdclzdgmdbgBsInfo::getChand7, bo.getChand7());
        lqw.eq(StringUtils.isNotBlank(bo.getBil1()), WdclzdgmdbgBsInfo::getBil1, bo.getBil1());
        lqw.eq(StringUtils.isNotBlank(bo.getBil2()), WdclzdgmdbgBsInfo::getBil2, bo.getBil2());
        lqw.eq(StringUtils.isNotBlank(bo.getBil3()), WdclzdgmdbgBsInfo::getBil3, bo.getBil3());
        lqw.eq(StringUtils.isNotBlank(bo.getBil4()), WdclzdgmdbgBsInfo::getBil4, bo.getBil4());
        lqw.eq(StringUtils.isNotBlank(bo.getBil5()), WdclzdgmdbgBsInfo::getBil5, bo.getBil5());
        lqw.eq(StringUtils.isNotBlank(bo.getBil6()), WdclzdgmdbgBsInfo::getBil6, bo.getBil6());
        lqw.eq(StringUtils.isNotBlank(bo.getBil7()), WdclzdgmdbgBsInfo::getBil7, bo.getBil7());
        lqw.eq(bo.getGanmdpjz() != null, WdclzdgmdbgBsInfo::getGanmdpjz, bo.getGanmdpjz());
        return lqw;
    }

    /**
     * 新增最大干密度最佳含水率试验检测报告，详细使用对应记录的详细
     */
    @Override
    public Boolean insertByBo(WdclzdgmdbgBsInfoBo bo) {
        WdclzdgmdbgBsInfo add = BeanUtil.toBean(bo, WdclzdgmdbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 3次
            ArrayList<WdclzdgmdBaDetail> details = new ArrayList<>(3);
            for (long i = 1; i <= 3L; i++) {
                details.add(new WdclzdgmdBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改最大干密度最佳含水率试验检测报告，详细使用对应记录的详细
     */
    @Override
    @ComputeReportResult
    public Boolean updateByBo(WdclzdgmdbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        WdclzdgmdbgBsInfo update = BeanUtil.toBean(bo, WdclzdgmdbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), WdclzdgmdBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(WdclzdgmdbgBsInfoBo bo) {
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdclzdgmdbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除最大干密度最佳含水率试验检测报告，详细使用对应记录的详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 生成 最大干密度最佳含水率试验检测报告 检测结论
     */
    @Override
    public WdclzdgmdbgBsInfoVo jcjl(WdclzdgmdbgBsInfoVo vo) {
//        if (StrUtil.isEmpty(vo.getPandyj())) {
//            return vo;
//        }
        vo.setJiancjl("检测结论：经检测，室内最大干密度平均值为"+BigDecimalUtil.stpTrailZero(vo.getGanmdpjz()) + "g/cm3，确定现场压实度用室内最大干密度乘1.01系数为" +
            BigDecimalUtil.stpTrailZero(BigDecimalUtil.multiply(new BigDecimal("1.01") , vo.getGanmdpjz(), 3)) + "g/cm3. 符合《水泥稳定碎石基层施工技术规范》"
            + "DB37/T 3577-2019" + "及设计文件要求。");
        return vo;
    }
}
